package com.touchstone.synchdata.common.util;


import com.github.promeg.pinyinhelper.Pinyin;

import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description : 工具类
 * @Author : hunter
 * @Date: 2019-11-21 17:05
 * @Version: 1.0
 **/
public class CommonUtils {

    /**
     * 手机号校验正则表达式
     */
    public static final String REGEXP_PHONE = "^[1][3,4,5,7,8][0-9]{9}$";

    /**
     * 邮箱校验正则表达式
     */
    public static final String REGEXP_EMAIL = "^([a-z0-9A-Z]+[-|\\.|_]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    /**
     * 手机号数字校验
     */
    public static final String REGEXP_PHONE_NUM = "^[0-9]{3,11}$";

    /**
     * 判断对象为空，
     *
     * @return true:是  false:否；注意isEmpty(0)返回的是true
     * @author hunter
     * @date: 2019/11/6 9:58
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;

        } else if (obj instanceof String && (obj.toString().trim().equals(""))) {
            return true;

        } /*else if (obj instanceof Number && ((Number) obj).doubleValue() == 0) {
            return true;

        } */ else if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
            return true;

        } else if (obj instanceof Map && ((Map) obj).isEmpty()) {
            return true;

        } else if (obj instanceof Object[] && ((Object[]) obj).length == 0) {
            return true;

        } else {
            return false;
        }
    }

    /**
     * 判断是否是邮箱
     *
     * @param email
     * @return true:是  false:否
     * @author hunter
     * @date: 2019/11/6 10:10
     */
    public static boolean isEmail(String email) {
        if (isEmpty(email)) {
            return false;
        }
        boolean tag = true;
        final Pattern pattern = Pattern.compile(REGEXP_EMAIL);
        final Matcher mat = pattern.matcher(email);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
    }

    /**
     * 判断是否为11位大陆手机号
     *
     * @param phone 手机号码
     * @return true:是  false:否
     * @author hunter
     * @date: 2019/11/6 10:10
     */
    public static boolean isMobilePhone(String phone) {
        if (isEmpty(phone)) {
            return false;
        }
        int phoneLength = 11;
        if (phone.length() != phoneLength) {
            return false;
        }
        return rexExpCheck(REGEXP_PHONE, phone);
    }

    /**
     * 判断对象不为空
     *
     * @return true:是  false:否；注意isNotEmpty(0)返回的是false
     * @author hunter
     * @date: 2019/11/6 9:58
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 获取字符串拼音首字母
     *
     * @param str 传入的字符串
     * @return String 拼音首字母
     * @author hunter
     * @date: 2019/11/21 16:48
     */
    public static String getPY(String str) {
        String temp;
        StringBuilder sb = new StringBuilder();
        for (char c : str.toCharArray()) {
            temp = String.valueOf(c);
            if (!"".equals(temp.trim())) {
                sb.append(Pinyin.toPinyin(c).toLowerCase().toCharArray()[0]);
            }
        }
        return sb.toString();
    }

    /**
     * 正则校验
     *
     * @param pattern 正则表达式
     * @param text    校验内容
     * @return true:是;false:否
     * @author hunter
     * @date: 2019/11/6 10:18
     */
    public static boolean rexExpCheck(String pattern, String text) {
        Pattern idCardPattern = Pattern.compile(pattern);
        Matcher m = idCardPattern.matcher(text);
        boolean isMatch = m.matches();
        if (!isMatch) {
            return false;
        }
        return true;
    }

    /**
     * 根据id字符串获取id集合
     *
     * @param
     * @return
     * @author July
     * @createTime 2020-03-03 9:24
     **/
    public static Long[] getIdArray(String idsStr) {
        String comma = ",";
        if (!idsStr.contains(comma)) {
            return new Long[]{Long.valueOf(idsStr)};
        }
        String[] idStrArray = idsStr.split(comma);
        Long[] idArray = new Long[idStrArray.length];
        for (int i = 0; i < idStrArray.length; i++) {
            idArray[i] = Long.valueOf(idStrArray[i]);
        }
        return idArray;
    }

    /**
     * 根据文件路径截取文件名
     *
     * @param filePath 文件路径
     * @return java.lang.String
     * @author djw
     * @date 2020/4/13 19:23
     */
    public static String getFileName(String filePath) {
        if (null == filePath || "".equals(filePath.trim())) {
            return null;
        }
        int i = filePath.lastIndexOf("/");
        if (i < 0) {
            return null;
        }
        return filePath.substring(i);
    }


    public static void main(String[] args) {
//        System.out.println(getPY("中国有 14亿人口。"));
//        Map<String,Object> map = new HashMap<>();
//        map.put("appCode","admin");
//        map.put("appSecret","9297d74b6dce4a321307610c68f33d44");
//        System.out.println(HttpUtils.get("http://localhost:8080/ex/auth/getToken",map));
//        System.out.println(EncryptUtils.md5("123"));
    }

}
